package servlet;

import Dao.JdbcTools;
import com.alibaba.fastjson.JSON;
import model.VueStu;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/showStu")
public class VueTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        try {
            List<VueStu> vueStus = JdbcTools.queryBeanList("select * from vue", VueStu.class);
            String res = JSON.toJSONString(vueStus);
            System.out.println("后端json格式的字符串="+res);
            //传回前端json格式的字符串key--value
            PrintWriter writer = resp.getWriter();
            writer.write(res);
            //释放缓存和对象
            writer.flush();
            writer.close();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
